/**
 * 
 */
package org.hitchhike.common.utils;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 * <b>WritePhotoUtils</b> 是向客户端输出图片数据的工具类
 * </p>
 *
 * @since 2015-11-26
 * @author yx
 * @version $Id: WritePhotoUtils.java 58221 2016-08-17 00:28:30Z foreveross_liangkangzhu $
 *
 */
public class WritePhotoUtils {

	public static void writePhotoData(final HttpServletResponse response, String type, byte[] data) throws IOException {
		response.setContentType("image/" + type);
		OutputStream outputSream = response.getOutputStream();
		InputStream in = new ByteArrayInputStream(data);
		int len = 0;
		byte[] buf = new byte[1024];
		while ((len = in.read(buf, 0, 1024)) != -1) {
			outputSream.write(buf, 0, len);
		}
		outputSream.close();
	}

	public static void writePhotoData(final HttpServletResponse response, String type, InputStream in)
			throws IOException {
		response.setContentType("image/" + type);
		OutputStream outputSream = response.getOutputStream();
		int len = 0;
		byte[] buf = new byte[1024];
		while ((len = in.read(buf, 0, 1024)) != -1) {
			outputSream.write(buf, 0, len);
		}
		outputSream.close();
	}

	public static void writePhotoDataDefault(final HttpServletResponse response, String type, byte[] data)
			throws IOException {
		InputStream in = null;
		if (data == null) {
			response.setContentType("image/jpg");
			in = WritePhotoUtils.class.getResourceAsStream("none.jpg");
		} else {
			response.setContentType("image/" + type);
			in = new ByteArrayInputStream(data);
		}
		OutputStream outputSream = response.getOutputStream();
		int len = 0;
		byte[] buf = new byte[1024];
		while ((len = in.read(buf, 0, 1024)) != -1) {
			outputSream.write(buf, 0, len);
		}
		outputSream.close();
	}

	/**
	 * add by ky_qbq 2015-7-21 
	 * 获取无限极公司盖章
	 */
	public static void writeInfinitusPhoto(final HttpServletResponse response) throws IOException {
		InputStream in = null;
		response.setContentType("image/png");
		in = WritePhotoUtils.class.getResourceAsStream("/com/infinitus/gbss/base/core/utils/infintus.png");

		OutputStream outputSream = response.getOutputStream();
		int len = 0;
		byte[] buf = new byte[1024];
		while ((len = in.read(buf, 0, 1024)) != -1) {
			outputSream.write(buf, 0, len);
		}
		outputSream.close();
	}
}
